package com.sinobpo.rnd2.ehr.web.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.sinobpo.rnd2.ehr.dao.MedExpSatDao;
import com.sinobpo.rnd2.ehr.model.MedExpSat;
import com.sinobpo.rnd2.ehr.util.CommonUtil;
import com.sinobpo.rnd2.ehr.util.JsonDateValueProcessor;
import com.sinobpo.rnd2.ehr.util.MedExpSatType;
import com.sinobpo.rnd2.ehr.util.PieDataType;
import com.sinobpo.rnd2.ehr.util.TimeType;

/**
 * 医疗费用管理类
 * 
 * @author yinghui.zhang
 * @CreateDate 2012-7-23
 * @file MedExpSatManageAction.java
 * @package com.sinobpo.rnd2.ehr.web.action
 * @version 1.0
 * @Email uuhui@163.com
 */
@Controller("medExpSatManageAction")
public class MedExpSatManageAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 序列号
	 */
	@Resource(name = "medExpSatDaoImpl")
	private MedExpSatDao medExpSatDao;
	private JSONObject jsonObj;
	private JSONArray jsonArray;
	private String orgCode; // 机构代码
	private String startDate;// 开始时间
	private String endDate;// 截止时间
	private int page; // jquery easyUi dataGrid 用于分页数据的当前页数
	private int rows; // jquery easyUI dataGrid 用于分页数据的每页数据
	/**
	 * 病人类型[自费/医保/农保]
	 */
	private String patientType;
	/**
	 * 费用类型 [诊疗/药品/其它]
	 */
	private String medExpType;
	/**
	 * 统计类型(按门诊/住院分类
	 */
	private String satType;

	/**
	 * 设置日期格式
	 * 
	 * @return
	 */
	private TimeType getTimeType() {
		if (startDate.length() > 4)
			return TimeType.DAY;
		else if (startDate.length() == 2)
			return TimeType.MONTH;
		else
			return TimeType.YEAR;
	}

	/**
	 * 按病人分类统计的饼图信息
	 * 
	 * @return String
	 */
	public String getSumPieByPatient() {
		/*
		 * conds 包括: 1.费用汇总类型[按病人类型汇总,按费用类型汇总] 2.机构代码 3.开始日期4.结束日期 5.统计类别(门诊,住院)
		 * 6.费用汇总类型[按病人类型汇总,按费用类型汇总] 7.费用汇总下的分类([自费,医保,农保],[诊疗,药品,其它])
		 * 注:费用汇总下的分类不存在默认按总类汇总(不分类)
		 */
		if (medExpType != null)
			medExpType = null;
		Object[] obj = (Object[]) medExpSatDao.getSumByType(getTimeType(),
				MedExpSatType.PatientSat.getValue(), orgCode, startDate,
				endDate, satType, MedExpSatType.MedExpSat.getValue(),
				patientType);
		if(obj.length==3)
		{
			if (obj[0] == null && obj[1] == null && obj[2] == null)
				jsonArray = null;
			else
				jsonArray = JSONArray.fromObject(CommonUtil.getPieChartData(obj,
						PieDataType.PatientPie));
		}else{
			if (obj[0] == null && obj[1] == null )
				jsonArray = null;
			else
				jsonArray = JSONArray.fromObject(CommonUtil.getPieChartData(obj,
						PieDataType.PatientPie));
		}
		return "jsonArray";
	}

	/**
	 * 按费用分类统计的饼图信息
	 * 
	 * @return String
	 */
	public String getSumPieByMedExp() {
		/*
		 * conds 包括: 1.费用汇总类型[按病人类型汇总,按费用类型汇总] 2.机构代码 3.开始日期4.结束日期 5.统计类别(门诊,住院)
		 * 6.费用汇总类型[按病人类型汇总,按费用类型汇总] 7.费用汇总下的分类([自费,医保,农保],[诊疗,药品,其它])
		 * 注:费用汇总下的分类不存在默认按总类汇总(不分类)
		 */
		if (patientType != null)
			patientType = null;
		Object[] obj = (Object[]) medExpSatDao.getSumByType(getTimeType(),
				MedExpSatType.MedExpSat.getValue(), orgCode, startDate,
				endDate, satType, MedExpSatType.PatientSat.getValue(),
				medExpType);
		if(obj.length==3){
		if (obj[0] == null && obj[1] == null && obj[2] == null)
			jsonArray = null;
		else
			jsonArray = JSONArray.fromObject(CommonUtil.getPieChartData(obj,
					PieDataType.MedExpTypePie));
		}else{
			if (obj[0] == null && obj[1] == null )
				jsonArray = null;
			else
				jsonArray = JSONArray.fromObject(CommonUtil.getPieChartData(obj,
						PieDataType.MedExpTypePie));
		}
		return "jsonArray";
	}

	/**
	 * 按门诊人均费用统计的饼图信息
	 * 
	 * @return
	 */
	public String getSumPieByClinic() {
		Object[] obj = (Object[]) medExpSatDao.getSumByTypeOfPCT(getTimeType(),
				MedExpSatType.ClinicView.getValue(), orgCode, startDate,
				endDate);
		if(obj.length==3){
		if (obj[0] == null && obj[1] == null && obj[2] == null)
			jsonArray = null;
		else
			jsonArray = JSONArray.fromObject(CommonUtil.getPieChartData(obj,
					PieDataType.PatientPie));
		}else{
			if (obj[0] == null && obj[1] == null )
				jsonArray = null;
			else
				jsonArray = JSONArray.fromObject(CommonUtil.getPieChartData(obj,
						PieDataType.PatientPie));
		}
		return "jsonArray";
	}

	/**
	 * 按住院人均费用统计的饼图信息
	 * 
	 * @return
	 */
	public String getSumPieByHospital() {
		Object[] obj = (Object[]) medExpSatDao.getSumByTypeOfPCT(getTimeType(),
				MedExpSatType.HosNumView.getValue(), orgCode, startDate,
				endDate);
		if(obj.length==3){
		if (obj[0] == null && obj[1] == null && obj[2] == null)
			jsonArray = null;
		else
			jsonArray = JSONArray.fromObject(CommonUtil.getPieChartData(obj,
					PieDataType.PatientPie));
		}else{
			if (obj[0] == null && obj[1] == null )
				jsonArray = null;
			else
				jsonArray = JSONArray.fromObject(CommonUtil.getPieChartData(obj,
						PieDataType.PatientPie));
		}
		return "jsonArray";
	}

	/**
	 *按病人分类统计column及line图表
	 * 
	 * @return
	 */
	public String getSumChartByPaitent() {
		if (medExpType != null)
			medExpType = null;
		List<MedExpSat> list = medExpSatDao.getSum(getTimeType(),
				MedExpSatType.PatientSat.getValue(), orgCode, startDate,
				endDate, satType, MedExpSatType.MedExpSat.getValue(),
				patientType);
		if (list.size() != 0)
			jsonObj = JSONObject.fromObject(CommonUtil.getChartOfMedExpSatData(
					satType, patientType, medExpType, MedExpSatType.PatientSat,
					getTimeType(), list));
		else
			jsonObj = null;
		return SUCCESS;
	}

	/**
	 * 按费用分类统计column及line图表
	 * 
	 * @return
	 */
	public String getSumChartByMedExp() {
		if (patientType != null)
			patientType = null;
		List<MedExpSat> list = medExpSatDao.getSum(getTimeType(),
				MedExpSatType.MedExpSat.getValue(), orgCode, startDate,
				endDate, satType, MedExpSatType.PatientSat.getValue(),
				medExpType);
		if (list.size() != 0)
			jsonObj = JSONObject.fromObject(CommonUtil.getChartOfMedExpSatData(
					satType, patientType, medExpType, MedExpSatType.MedExpSat,
					getTimeType(), list));
		else
			jsonObj = null;
		return SUCCESS;
	}

	/**
	 * 按门诊人均费用统计column及line图表
	 * 
	 * @return
	 */
	public String getSumChartByClinic() {
		List<MedExpSat> list = medExpSatDao.getSumOfPCT(getTimeType(),
				MedExpSatType.ClinicView.getValue(), orgCode, startDate,
				endDate);
		if (list.size() != 0)
			jsonObj = JSONObject.fromObject(CommonUtil
					.getChartOfPCTMedExpSatData(getTimeType(), list));
		else
			jsonObj = null;
		return SUCCESS;
	}

	/**
	 * 按住院人均费用统计column及line图表
	 * 
	 * @return
	 */
	public String getSumChartByHospital() {
		List<MedExpSat> list = medExpSatDao.getSumOfPCT(getTimeType(),
				MedExpSatType.HosNumView.getValue(), orgCode, startDate,
				endDate);
		if (list.size() != 0)
			jsonObj = JSONObject.fromObject(CommonUtil
					.getChartOfPCTMedExpSatData(getTimeType(), list));
		else
			jsonObj = null;
		return SUCCESS;
	}

	/**
	 * 查询数据明细信息
	 * 
	 * @return
	 */
	public String getSumList() {
		if (patientType != null && patientType.equals("null"))
			patientType = null;
		if (medExpType != null && medExpType.equals("null"))
			medExpType = null;
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,
				new JsonDateValueProcessor());
		List<MedExpSat> list = medExpSatDao.getList(page, rows, getTimeType(),
				orgCode, startDate, endDate, satType, patientType, medExpType);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", medExpSatDao.getCount(getTimeType(), orgCode,
				startDate, endDate, satType, patientType, medExpType));
		map.put("rows", list);
		jsonObj = JSONObject.fromObject(map, jsonConfig);
		jsonConfig = null;
		return SUCCESS;
	}

	/**
	 * 查询门诊人均费用明细信息
	 * 
	 * @return
	 */
	public String getSumListByCLinic() {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,
				new JsonDateValueProcessor());
		List<MedExpSat> list = medExpSatDao.getListOfPCT(page, rows,
				getTimeType(), MedExpSatType.ClinicView.getValue(), orgCode,
				startDate, endDate);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", medExpSatDao.getCountOfPCT(getTimeType(),
				MedExpSatType.ClinicView.getValue(), orgCode, startDate,
				endDate));
		map.put("rows", list);
		jsonObj = JSONObject.fromObject(map, jsonConfig);
		jsonConfig = null;
		return SUCCESS;
	}

	/**
	 * 查询住院人均费用明细信息
	 * 
	 * @return
	 */
	public String getSumListByHospital() {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,
				new JsonDateValueProcessor());
		List<MedExpSat> list = medExpSatDao.getListOfPCT(page, rows,
				getTimeType(), MedExpSatType.HosNumView.getValue(), orgCode,
				startDate, endDate, satType, patientType, medExpType);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", medExpSatDao.getCountOfPCT(getTimeType(),
				MedExpSatType.HosNumView.getValue(), orgCode, startDate,
				endDate));
		map.put("rows", list);
		jsonObj = JSONObject.fromObject(map, jsonConfig);
		jsonConfig = null;
		return SUCCESS;
	}

	public JSONObject getJsonObj() {
		return jsonObj;
	}

	public void setJsonObj(JSONObject jsonObj) {
		this.jsonObj = jsonObj;
	}

	public JSONArray getJsonArray() {
		return jsonArray;
	}

	public void setJsonArray(JSONArray jsonArray) {
		this.jsonArray = jsonArray;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getPatientType() {
		return patientType;
	}

	public void setPatientType(String patientType) {
		this.patientType = patientType;
	}

	public String getMedExpType() {
		return medExpType;
	}

	public void setMedExpType(String medExpType) {
		this.medExpType = medExpType;
	}

	public String getSatType() {
		return satType;
	}

	public void setSatType(String satType) {
		this.satType = satType;
	}
}
